home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9135 < prev    next >
Encoding:
Text File  |  1996-08-05  |  10.4 KB  |  469 lines

  1. Path: lantana.singnet.com.sg!usenet
  2. From: Teddy Bear <s7700038@singnet.com.sg>
  3. Newsgroups: comp.lang.c
  4. Subject: Can C save and retreive from disk?
  5. Date: 8 Mar 1996 13:34:28 GMT
  6. Organization: Singapore Telecom Internet Service
  7. Message-ID: <4hpd14$hel@lantana.singnet.com.sg>
  8. NNTP-Posting-Host: ts900-3729.singnet.com.sg
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed;
  11.     boundary="-------------------------------300002593617914"
  12. X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
  13.  
  14. This is a multi-part message in MIME format.
  15.  
  16. ---------------------------------300002593617914
  17. Content-Transfer-Encoding: 7bit
  18. Content-Type: text/plain; charset=us-ascii
  19.  
  20. Can anyone teach me or refer me or help in any way?
  21. I need to save to disk and retrieve from disk
  22. Can anyone help me
  23. The prog goes
  24.  
  25. ---------------------------------300002593617914
  26. Content-Transfer-Encoding: 7bit
  27. Content-Type: text/plain
  28.  
  29. /* This program simply enable the user to enter the particular(s). It will
  30.    then sort the name in an alphabetical order and display the particular(s)
  31.    on the screen. It also ensures that the user is able to add and delete
  32.    the particular(s). */
  33.  
  34. #include <stdio.h>
  35. #include <alloc.h>
  36. #include <conio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #define TRUE  1
  40. #define FALSE 0
  41.  
  42. typedef struct data{
  43.     int age;
  44.     char name[40];
  45.     char address[100];
  46.     struct data *next;
  47. }dataelm;
  48. typedef dataelm* dataptr;
  49.  
  50. void createdata(dataptr *ptr_to_head, dataptr head);
  51. int  countlink(dataptr head);
  52. void sortlink(dataptr head);
  53. dataptr searchlink(dataptr head, char *wanted);
  54. void display(dataptr head);
  55. void save(dataptr headptr, char file[20]);
  56. void read(char file[20]);
  57. dataptr clearlink(dataptr head);
  58. dataptr addnode(dataptr head);
  59. dataptr delnode(dataptr head);
  60. void check(dataptr head);
  61.  
  62. int main()
  63. {
  64.     char *sname, option;
  65.     int checker = 0;
  66.     dataptr ptr, *ptr_to_head, head, tail;
  67.  
  68.     head = NULL;     /* assigns head with NULL, starting of link list */
  69.     option = '1';
  70.     do
  71.     {
  72.     clrscr();
  73.     printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ  MAIN MENU  ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ\n");
  74.     printf("\n1) Adding of particular(s).\n");
  75.     printf("2) Adding of new particular(s).\n");
  76.     printf("3) Deleting of current particular(s).\n");
  77.     printf("4) Display all the particular(s) entered.\n");
  78.     printf("5) Exit the program.\n");
  79.     printf("\nPlease enter your choice : ");
  80.     if(option < '0' || option > '6')
  81.         printf("\nInvaild option. Please re-enter.");
  82.     option = getche();
  83.     switch(option)
  84.     {
  85.         case '1' : if(checker != 0)
  86.                {
  87.                printf("\a");
  88.                printf("\n\nYou had create a link list previously.");
  89.                printf("\nIf you proceed, the previous data will be lost.");
  90.                printf("\n\nEnter a 'Y' to proceed.");
  91.                printf("\nEnter ESC to return to main menu ");
  92.                if((toupper(getch())) != 'Y')
  93.                    break;
  94.                else
  95.                    clearlink(head);
  96.                }
  97.                createdata(ptr_to_head, NULL);
  98.                head = *ptr_to_head;
  99.                sortlink(head);
  100.                display(head);
  101.                checker = 1;
  102.                break;
  103.         case '2' : if(checker != 1)
  104.                {
  105.                printf("\a");
  106.                printf("\n\nYou have not yet create a link list.");
  107.                printf("\nPlease choose option '1'.");
  108.                printf("\n\nPress any key to continue.");
  109.                getch();
  110.                break;
  111.                }
  112.                head = addnode(head);
  113.                checker = 1;
  114.                display(head);
  115.                break;
  116.         case '3' : if(checker!=1)
  117.                {
  118.                printf("\a");
  119.                printf("\n\nYou have not yet create a link list.");
  120.                printf("\nPlease choose option '1'.");
  121.                printf("\n\nPress any key to continue.");
  122.                getch();
  123.                break;
  124.                }
  125.                display(head);
  126.                head = delnode(head);
  127.                display(head);
  128.                if(head == NULL)
  129.                checker = 0;
  130.                break;
  131.         case '4' : display(head);
  132.                break;
  133.         case '5' : printf("\n\nHave a nice day. BYE!\n");
  134.                printf("Press any key to end...");
  135.                getch();
  136.                exit(0);
  137.     }
  138.     }
  139.     while(option != '5');
  140.     return 0;
  141. }
  142.  
  143. void createdata(dataptr *ptr_to_head, dataptr head)
  144. /* create a link of the telephone list */
  145. {
  146.     dataptr temp, last;
  147.     char ans;
  148.     int elm_size = sizeof(dataelm);
  149.     do
  150.     {
  151.     temp = (dataptr)malloc(elm_size);
  152.     clrscr();
  153.     printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ  PARTICULAR CREATING  ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ\n");
  154.     printf("\nEnter name to be added   : ");
  155.     gets(temp->name);
  156.     printf("Enter Age                : ");
  157.     scanf("%d",&temp->age);
  158.     fflush(stdin);
  159.     printf("Enter Address            : ");
  160.     gets(temp->address);
  161.  
  162.     if(head == NULL)
  163.     {
  164.         head = temp;      /* assigns the address of dataptr to head */
  165.         last = temp;
  166.     }
  167.     else
  168.     {
  169.         last->next=temp;
  170.         last = temp;
  171.     }
  172.     printf("\nAny more you would like to enter ? <Y/N> : ");
  173.     ans=toupper(getch());
  174.     }
  175.     while(ans != 'N');
  176.     last->next = NULL;
  177.     *ptr_to_head = head;
  178. }
  179.  
  180. void sortlink(dataptr head)
  181. /* sort the link in an alphabetical order */
  182. {
  183.     dataptr front, back;
  184.     struct data temp;
  185.     int pass, cnt, condition;
  186.     int element;
  187.     element = countlink(head);
  188.     pass = 0;
  189.     do
  190.     {
  191.     pass++;
  192.     condition = TRUE;
  193.     front = head->next;
  194.     back  = head;
  195.     for(cnt = 0; cnt < element - pass; cnt++)
  196.     {
  197.         /* compare the character */
  198.         if((strcmp((back->name), (front->name))) > 0)
  199.         {
  200.         condition = FALSE;
  201.         strcpy(temp.name, back->name);
  202.         temp.age = back->age;
  203.         strcpy(temp.address, back->address);
  204.         strcpy(back->name, front->name);
  205.         back->age = front->age;
  206.         strcpy(back->address, front->address);
  207.         strcpy(front->name, temp.name);
  208.         front->age = temp.age;
  209.         strcpy(front->address, temp.address);
  210.         }
  211.         back = front;
  212.         front = front->next;
  213.     }
  214.     }
  215.     while(condition==FALSE);
  216. }
  217.  
  218. dataptr searchlink(dataptr head, char *wanted)
  219. /* search the link for an element */
  220. {
  221.     int element, count, found, condition;
  222.     dataptr ptr;
  223.     element = countlink(head);
  224.     ptr = head;
  225.     found = FALSE;
  226.     condition = TRUE;
  227.     for(count = 0; (count < element) && (condition); count++)
  228.     {
  229.     if((strcmp(ptr->name, wanted)) == 0)
  230.     {
  231.         found = TRUE;
  232.         condition = FALSE;
  233.     }
  234.     else
  235.         if((strcmp(ptr->name, wanted)) > 0)
  236.         condition = FALSE;
  237.         else
  238.         {
  239.         ptr = ptr->next;
  240.         count++;
  241.         }
  242.     }
  243.  
  244.     if(found)
  245.     return ptr;
  246.     else
  247.     return NULL;
  248. }
  249.  
  250. void display(dataptr head)
  251. /* display the particular(s) in the link */
  252. {
  253.     int x = 0;
  254.     int y = 6;
  255.     dataptr ptr;
  256.     ptr = head;
  257.     clrscr();
  258.     gotoxy(1,3);printf("NAME               AGE     ADDRESS");
  259.     gotoxy(1,4);printf("");
  260.     gotoxy(1,2);printf("");
  261.     while(ptr!=NULL)
  262.     {
  263.     gotoxy(x+1,y);printf("%s",ptr->name);
  264.     gotoxy(x+20,y);printf("%d ",ptr->age);
  265.     gotoxy(x+28,y);printf("%s",ptr->address);
  266.     y++;
  267.     ptr = ptr->next;
  268.  
  269.     if(wherey() > 20) /* gives current vertical cursor position */
  270.     {
  271.         printf("\nPress any key to view next page.");
  272.         getch();
  273.         clrscr();
  274.     }
  275.     }
  276.     printf("\n\nPress any key to continue.. ");
  277.     getch();
  278. }
  279.  
  280. dataptr clearlink(dataptr head)
  281. /* clear all data and free all memory used by the data */
  282. {
  283.     dataptr ptr;
  284.     while(head != NULL)
  285.     {
  286.     ptr = head;
  287.     head = head->next;
  288.     free(ptr);
  289.     }
  290.     free(ptr);
  291.     return head;
  292. }
  293.  
  294. dataptr addnode(dataptr head)
  295. /* add a node to the link */
  296. {
  297.     dataptr front, back, newitem;
  298.     int check, cnt;
  299.     newitem = (dataptr)malloc(sizeof(dataelm));
  300.     clrscr();
  301.     sortlink(head);
  302.     printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ  PARTICULAR ADDING  ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ");
  303.     printf("\n\nEnter name to be added : ");
  304.     gets(newitem->name);
  305.     if((strcmp((back->name),(front->name))) == 0)
  306.     {
  307.     printf("\a");
  308.     printf("\n\nDUPLICATE COPY");
  309.     strcpy(back->name,front->name);
  310.     back->age = front->age;
  311.     strcpy(back->address,front->address);
  312.     getch();
  313.     }
  314.     fflush(stdin);
  315.     printf("Enter age              : ");
  316.     scanf("%d", &newitem->age);
  317.     fflush(stdin);
  318.     printf("Enter address          : ");
  319.     gets(newitem->address);
  320.     if((strcmp(newitem->name, head->name)) <= 0)
  321.     {
  322.     newitem->next = head;
  323.     head = newitem;
  324.     }
  325.     else
  326.     {
  327.     back = head;
  328.     front = back->next;
  329.     while(front != NULL)
  330.     {
  331.         if((strcmp(newitem->name, front->name)) > 0)
  332.         {
  333.         back = front;
  334.         front = front->next;
  335.         }
  336.         else
  337.         break;
  338.     }
  339.     newitem->next = front;
  340.     back->next = newitem;
  341.     }
  342.     return head;
  343. }
  344.  
  345. dataptr delnode(dataptr head)
  346. /* delete a node from a link */
  347. {
  348.     dataptr front, back;
  349.     char *tempname;
  350.     if(head != NULL)
  351.     {
  352.     back = head;
  353.     front = back->next;
  354.     }
  355.     else
  356.     {
  357.     printf("\a\nThere is nothing to be deleted in the link list.");
  358.     getch();
  359.     return NULL;
  360.     }
  361.     tempname = (char *)malloc(sizeof(char *));
  362.     clrscr();
  363.     printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ  DATA TO BE DELETED  ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ");
  364.     printf("\n\nEnter name to be delete : ");
  365.     gets(tempname);
  366.     if((strcmp(tempname,back->name)) == 0)
  367.     {
  368.     front = head;
  369.     back = back->next;
  370.     free(front);
  371.     return back;
  372.     }
  373.     else
  374.     {
  375.     while(((strcmp(tempname, front->name)) != 0) && (front != NULL))
  376.     {
  377.         back = front;
  378.         front = front->next;
  379.     }
  380.     if(front == NULL)
  381.     {
  382.         printf("\a\nThere is no such name in the link.");
  383.         getch();
  384.         return head;
  385.     }
  386.     else
  387.     {
  388.         back->next = front->next;
  389.         free(front);
  390.         return head;
  391.     }
  392.     }
  393.  
  394. }
  395.  
  396. int countlink(dataptr head)
  397. /* count the number of elements in the link */
  398. {
  399.     int count;
  400.     dataptr ptr;
  401.     ptr = head;
  402.     if(ptr != NULL)
  403.     {
  404.     for(count = 1; ptr->next != NULL; count++)
  405.     ptr = ptr->next;
  406.     return count;
  407.     }
  408.     else
  409.     {
  410.     return 0;
  411.     }
  412. }
  413.  
  414. void save(dataptr headptr, char file[20])
  415. /* save particular(s) entered */
  416. {
  417.     FILE *f;
  418.     dataptr ptr;
  419.     ptr = headptr;
  420.     if(ptr == NULL)
  421.     {
  422.     printf("\aWarning! There is no link list to be saved\n");
  423.     }
  424.     else
  425.     {
  426.     printf("Enter a name for the file to be saved : ");
  427.     flushall();
  428.     gets(file);
  429.     f = fopen(file, "w");
  430.     while(ptr != NULL)
  431.     {
  432.         fprintf(f,"\nName    : %s\n", ptr->name);
  433.         fprintf(f,"Age     : %d\n", ptr->age);
  434.         fprintf(f,"Address : %s\n", ptr->address);
  435.         ptr = ptr->next;
  436.     }
  437.     printf("The data has been saved.\n");
  438.     fclose(f);
  439.     }
  440.     return;
  441. }
  442.  
  443. void read(char file[20])
  444. /* read particular(s) */
  445. {
  446.     FILE *f;
  447.     char msg[20];
  448.     printf("Enter the name of the file to be read : ");
  449.     flushall();
  450.     gets(file);
  451.     if((f = fopen(file, "r")) == NULL)
  452.     {
  453.     printf("\aError! Cannot open file! File does not exist.\n");
  454.     }
  455.     else
  456.     {
  457.     printf("The file consists of : ");
  458.     while(feof(f) == 0)
  459.     {
  460.         fgets(msg, 2, f);
  461.         printf("%s", msg);
  462.     }
  463.     fclose(f);
  464.      }
  465.      return;
  466. }
  467.  
  468. ---------------------------------300002593617914--
  469.